package com.unisyou.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.unisyou.utils.DateUtils;
import lombok.Data;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Date;

/**
 * 微信公众号用户实体类
 */
@Data
public class WXUsersBean {
    private Long id;
    private String openId;
    private Integer sex;
    private String language;
    private String nickName;
    private String country;
    private String province;
    private String city;
    private String headImgUrl;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date subscribeTime;
    private Integer subscribe;
    private String mobile;
    private String unionid;
    private Integer groupid;
    private String tagidList;
    private String subscribeScene;
    private String qrScene;
    private String qrSceneStr;
    private String remark;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date createdTime;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private Date updatedTime;
    private Integer state;

    /**
     * jar包自带的实体类WxMpUser转换成WXUsersBean
     * @param wxMpUser
     * @return
     */
    public static WXUsersBean transfer(WxMpUser wxMpUser){
        WXUsersBean wxUsersBean = new WXUsersBean();
        wxUsersBean.setOpenId(wxMpUser.getOpenId());
        if(wxMpUser.getSex() != null){
            wxUsersBean.setSex(wxMpUser.getSex());
        }
        if(!StringUtils.isBlank(wxMpUser.getLanguage())){
            wxUsersBean.setLanguage(wxMpUser.getLanguage());
        }
        if(!StringUtils.isBlank(wxMpUser.getNickname())){
            wxUsersBean.setNickName(wxMpUser.getNickname());
        }
        if(!StringUtils.isBlank(wxMpUser.getCountry())){
            wxUsersBean.setCountry(wxMpUser.getCountry());
        }
        if(!StringUtils.isBlank(wxMpUser.getProvince())){
            wxUsersBean.setProvince(wxMpUser.getProvince());
        }
        if(!StringUtils.isBlank(wxMpUser.getCity())){
            wxUsersBean.setCity(wxMpUser.getCity());
        }
        if(!StringUtils.isBlank(wxMpUser.getHeadImgUrl())){
            wxUsersBean.setHeadImgUrl(wxMpUser.getHeadImgUrl());
        }
        if(wxMpUser.getSubscribeTime() != null){

            wxUsersBean.setSubscribeTime(DateUtils.timestampToDate(String.valueOf(wxMpUser.getSubscribeTime())));
        }
        if(wxMpUser.getSubscribe()){
            wxUsersBean.setSubscribe(1);
            wxUsersBean.setUpdatedTime(null);
            wxUsersBean.setMobile("");
        }else{
            wxUsersBean.setSubscribe(2);
        }
        if(!StringUtils.isBlank(wxMpUser.getUnionId())){
            wxUsersBean.setUnionid(wxMpUser.getUnionId());
        }
        if(wxMpUser.getGroupId() != null){
            wxUsersBean.setGroupid(wxMpUser.getGroupId());
        }
        if(wxMpUser.getTagIds() != null){
            wxUsersBean.setTagidList(Arrays.toString(wxMpUser.getTagIds()));
        }
        if(!StringUtils.isBlank(wxMpUser.getSubscribeScene())){
            wxUsersBean.setSubscribeScene(wxMpUser.getSubscribeScene());
        }
        if(!StringUtils.isBlank(wxMpUser.getQrScene())){
            wxUsersBean.setQrScene(wxMpUser.getQrScene());
        }
        if(!StringUtils.isBlank(wxMpUser.getQrSceneStr())){
            wxUsersBean.setQrSceneStr(wxMpUser.getQrSceneStr());
        }
        if(!StringUtils.isBlank(wxMpUser.getRemark())){
            wxUsersBean.setRemark(wxMpUser.getRemark());
        }
        return wxUsersBean;
    }
}
